package com.prs.crm.action.stock;

import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductUnit;
import com.prs.crm.domain.stock.ProductVariable;
import com.prs.crm.service.stock.ProductUnitService;
import com.prs.crm.service.stock.ProductVariableService;

public class ProductUnitAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4672828928143744760L;
	private ProductVariable productVariable;
	private ProductUnit productUnit;
	
	private Collection<ProductUnit> units;
	private Collection<ProductVariable> variables;
	private ProductVariableService productVariableService;
	private ProductUnitService  productUnitService;
	private PaginationSupport  pagination;

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public ProductVariable getProductVariable() {
		return productVariable;
	}

	public void setProductVariable(ProductVariable productVariable) {
		this.productVariable = productVariable;
	}



	public Collection<ProductUnit> getUnits() {
		return units;
	}

	public void setUnits(Collection<ProductUnit> units) {
		this.units = units;
	}

	public Collection<ProductVariable> getVariables() {
		return variables;
	}

	public void setVariables(Collection<ProductVariable> variables) {
		this.variables = variables;
	}
	public ProductUnit getProductUnit() {
		return productUnit;
	}

	public void setProductUnit(ProductUnit productUnit) {
		this.productUnit = productUnit;
	}

	public ProductVariableService getProductVariableService() {
		return productVariableService;
	}

	public void setProductVariableService(
			ProductVariableService productVariableService) {
		this.productVariableService = productVariableService;
	}

	public ProductUnitService getProductUnitService() {
		return productUnitService;
	}

	public void setProductUnitService(ProductUnitService productUnitService) {
		this.productUnitService = productUnitService;
	}
	@Action ("listUnits")
	public String listUnits(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ProductUnit.class);
		this.setPagination(this.getProductUnitService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	@Action(value="saveUnit",results={@Result(name="success",type="redirect",location="listUnits"),
			@Result(name="error",type="chain",location="editUnit")})
	public String saveUnit(){
		if(productUnit.getUnitName()==null||productUnit.getUnitName().equals("")){
			this.addActionError("名称不能为空");
			return ERROR;
		}
		Collection<ProductUnit> productUnits=this.getProductUnitService().getAllproductUnits();
		for(ProductUnit productUnit1:productUnits){
			if(productUnit.getUnitName().equals(productUnit1.getUnitName())){
				this.addActionError("名称不能重复");
				return ERROR;
			}
		}
		//this.productUnit.setProductVariable(this.getProductVariable());
		this.getProductUnitService().save(productUnit);
		return SUCCESS;
	}
	@Action( value="editUnit")	
	public String editUnit(){
		this.variables=this.getProductVariableService().getAllProductVariables();
		return SUCCESS;
	}
	@Action(value="deleteUnit",results={@Result(name="success",type="redirect",location="listUnits")})
	public String deleteUnit(){
		this.getProductUnitService();
		return SUCCESS;
	}
}
